package cn.always.xiajia.framework.common.web;

import javax.servlet.ServletRequest;

/**
 * 专属于 web 包的工具类
 * 
 * @author xgj
 *
 */
public class WebFrameworkUtils {

	public static final String REQUEST_ATTRIBUTE_LOGIN_USER_ID = "login_user_id";

	public static final String REQUEST_ATTRIBUTE_LOGIN_USER_CODE = "login_user_code";

	public static final String REQUEST_ATTRIBUTE_LOGIN_USER_NAME = "login_user_name";

	public static final String REQUEST_ATTRIBUTE_LOGIN_USER_TYPE = "login_user_type";

	/**
	 * 设置到request
	 * 
	 * @param request
	 * @param userId 用户ID
	 */
	public static void setRqLoginUserId(ServletRequest request, Long userId) {
		request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_ID, userId);
	}

	/**
	 * 获取request的userId
	 * 
	 * @param request
	 * @param userId 用户ID
	 */
	public static Long getRqLoginUserId(ServletRequest request) {
		if (request == null) {
			return null;
		}
		return (Long) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_ID);
	}

	/**
	 * 设置到request
	 * 
	 * @param request
	 * @param userCode 用户编码
	 */
	public static void setRqLoginUserCode(ServletRequest request, String userCode) {
		request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_CODE, userCode);
	}

	/**
	 * 获取request的userCode
	 * 
	 * @param request
	 * @param userCode 用户编码
	 */
	public static String getRqLoginUserCode(ServletRequest request) {
		if (request == null) {
			return null;
		}
		return (String) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_CODE);
	}

	/**
	 * 设置到request
	 * 
	 * @param request
	 * @param userName 用户名称
	 */
	public static void setRqLoginUserName(ServletRequest request, String userName) {
		request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_CODE, userName);
	}

	/**
	 * 获取request的userName
	 * 
	 * @param request
	 * @param userName 用户名称
	 */
	public static String getRqLoginUserName(ServletRequest request) {
		if (request == null) {
			return null;
		}
		return (String) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_CODE);
	}

	/**
	 * 设置到request
	 * 
	 * @param request
	 * @param userType 用户类型
	 */
	public static void setRqLoginUserType(ServletRequest request, String userType) {
		request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_CODE, userType);
	}

	/**
	 * 获取request的userType
	 * 
	 * @param request
	 * @param userType 用户类型
	 */
	public static String getRqLoginUserType(ServletRequest request) {
		if (request == null) {
			return null;
		}
		return (String) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_CODE);
	}

}
